CXX := g++
SRC_DIR := .
MODE ?= release
BUILD_DIR = build/$(MODE)
BIN_DIR = bin/$(MODE)
TARGET = $(BIN_DIR)/interface_server.out

SRCS := main.cpp protocol.cpp server.cpp my_utility.cpp log.cpp database.cpp interface.cpp examiner_control.cpp
OBJS = $(SRCS:%.cpp=$(BUILD_DIR)/%.o)
DEPS = $(OBJS:.o=.d)

# 编译选项根据构建类型设置
ifeq ($(MODE),release)
CXXFLAGS = -O3 -DNDEBUG -std=c++20 -I$(SRC_DIR)
else
CXXFLAGS = -g -O0 -std=c++20 -I$(SRC_DIR)
endif

LDFLAGS := -ljsoncpp -pthread ../logger/bin/release/liblog.so -I/usr/local/mysqlcppconn/include -L/usr/local/mysqlcppconn/lib -lmysqlcppconn

# 无对应源文件的头文件
HEADER_DEPS := setting.hpp socket.hpp

.PHONY: all release debug clean build

all: debug

release:
	@$(MAKE) build MODE=release

debug:
	@$(MAKE) build MODE=debug

build: $(TARGET)

$(TARGET): $(OBJS)
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)

# 通用编译规则
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) -MMD -MP -Wall -Wextra -c $< -o $@

-include $(DEPS)

clean:
	rm -rf build bin